/*
 * @Author: herw
 * @Date: 2021-01-14 13:48:58
 * @Last Modified by: herw
 * @Last Modified time: 2021-01-19 10:37:16
 */
<template>
  <div class="app-container">
    <!--工具栏-->
    <div class="head-container">
      <div v-if="crud.props.searchToggle">
        <!-- 搜索 -->
        <el-input
          v-model="query.blurry"
          clearable
          placeholder="模糊搜索"
          style="width: 200px"
          class="filter-item"
          @keyup.enter.native="crud.toQuery"
        />
        <rrOperation />
      </div>
      <crudOperation :permission="permission" />
    </div>
    <!--表格渲染-->
    <el-table
      ref="table"
      v-loading="crud.loading"
      :data="crud.data"
      highlight-current-row
      stripe
      style="width: 100%"
      @selection-change="crud.selectionChangeHandler"
    >
      <el-table-column type="selection" width="55" />
      <el-table-column prop="name" label="名称" />
      <el-table-column width="130" prop="enable" label="Cluster status" />
      <el-table-column
        :show-overflow-tooltip="true"
        prop="zkAddress"
        label="zooKeeper"
      />
      <el-table-column
        :show-overflow-tooltip="true"
        prop="broker"
        label="Broker"
      />
      <el-table-column prop="kafkaVersion" label="Kafka" />
      <el-table-column prop="brokerSize" label="brokerSize" />
      <el-table-column prop="location" label="location" />
      <el-table-column prop="grafAddr" label="grafAddr" />
      <el-table-column prop="createTime" width="200px" label="创建日期">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column
        v-if="checkPermission(['admin', 'cluster:edit', 'cluster:del'])"
        label="操作"
        width="150px"
        align="center"
      >
        <template slot-scope="scope">
          <udOperation :data="scope.row" :permission="permission" />
        </template>
      </el-table-column>
    </el-table>
    <!--分页组件-->
    <pagination />
    <!--表单渲染-->
    <el-dialog
      append-to-body
      :close-on-click-modal="false"
      :before-close="crud.cancelCU"
      :visible.sync="crud.status.cu > 0"
      :title="crud.status.title"
      width="570px"
    >
      <el-form
        ref="form"
        :inline="true"
        :model="form"
        :rules="rules"
        size="small"
        label-width="116px"
      >
        <el-form-item label="集群名" prop="name">
          <el-input v-model="form.name" style="width: 370px" />
        </el-form-item>
        <el-form-item label="ZooKeeper" prop="zkAddress">
          <el-input
            v-model="form.zkAddress"
            placeholder="ip:port eg:localhost:2181"
            style="width: 370px"
          />
        </el-form-item>
        <el-form-item label="Kafka" prop="broker">
          <el-input
            v-model="form.broker"
            placeholder="ip:port eg:localhost:9092"
            style="width: 370px"
          />
        </el-form-item>
        <el-form-item label="Location" prop="location">
          <el-select
            v-model="form.location"
            style="width: 370px"
            placeholder="请选择数据范围"
          >
            <el-option
              v-for="item in locations"
              :key="item"
              :label="item"
              :value="item"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="Kafka Version" prop="kafkaVersion">
          <el-input
            v-model="form.kafkaVersion"
            placeholder="please input kafka version"
            style="width: 370px"
          />
        </el-form-item>
        <el-form-item label="高级配置" prop="enable">
          <el-switch
            v-model="form.enable"
            style="width: 370px"
            active-color="#409EFF"
            inactive-color="#F56C6C"
          />
        </el-form-item>
        <el-form-item
          v-show="form.enable"
          label="Broker Size"
          prop="brokerSize"
        >
          <el-input
            v-model="form.brokerSize"
            style="width: 370px"
            placeholder="please input broker count"
          />
        </el-form-item>
        <el-form-item v-show="form.enable" label="Grafana" prop="grafAddr">
          <el-input
            v-model="form.grafAddr"
            style="width: 370px"
            placeholder="please input grafana alert address"
          />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="text" @click="crud.cancelCU">取消</el-button>
        <el-button
          :loading="crud.status.cu === 2"
          type="primary"
          @click="crud.submitCU"
        >确认</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import CRUD, { presenter, header, form, crud } from '@crud/crud'
import rrOperation from '@crud/RR.operation'
import crudOperation from '@crud/CRUD.operation'
import udOperation from '@crud/UD.operation'
import pagination from '@crud/Pagination'
import crudCluster from '@/api/mq/kafka/cluster'
import { testZooKeeperConnect, testKafkaConnect } from '@/api/mq/kafka/connect'

const defaultForm = {}
export default {
  name: 'Cluster',
  components: { pagination, crudOperation, rrOperation, udOperation },
  cruds() {
    return CRUD({ title: '集群', url: 'api/mq/cluster', crudMethod: { ...crudCluster }})
  },
  mixins: [presenter(), header(), form(defaultForm), crud()],
  data() {
    const validateZooKeeper = (rule, value, callback) => {
      if (!value) {
        callback('required')
      } else if (value.trim() === '') {
        callback('please input a vaild value')
      } else {
        testZooKeeperConnect(this.form).then((res) => {
          if (res) {
            callback()
          } else {
            callback(new Error('zookeeper address inactive'))
          }
        }).catch(() => {
          callback(new Error('zookeeper address inactive'))
        })
      }
    }
    const validateKafka = (rule, value, callback) => {
      if (!value) {
        callback('required')
      } else if (value.trim() === '') {
        callback('please input a vaild value')
      } else {
        testKafkaConnect(this.form).then((res) => {
          if (res) {
            callback()
          } else {
            callback(new Error('kafka address inactive'))
          }
        }).catch(() => {
          callback(new Error('kafka address inactive'))
        })
      }
    }
    return {
      locations: ['dev', 'prod'],
      permission: {
        add: ['admin', 'cluster:add'],
        edit: ['admin', 'cluster:edit'],
        del: ['admin', 'cluster:del']
      },
      rules: {
        name: [
          { required: true, message: '请输入集群名', trigger: 'blur' },
          { min: 2, max: 20, message: '长度在 2 到 20 个字符', trigger: 'blur' }
        ],
        zkAddress: [{ required: true, trigger: 'blur', validator: validateZooKeeper }],
        broker: [{ required: true, trigger: 'blur', validator: validateKafka }]
      }
    }
  },
  methods: {

  }
}
</script>
